/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin.model;

import org.grenader.jscssmin.JsCssMinConfig;
import org.grenader.jscssmin.JsCssProcessor;
import org.grenader.jscssmin.ResourceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 25, 2009
 */
public class ResourceBlock {
    private List<Resource> resourceList = new ArrayList<Resource>();
    private Map<ResourceType, ResourceGroup> typesMap = new TreeMap<ResourceType, ResourceGroup>();

    public ResourceBlock() {
    }

    public void add(Resource resource) {
        resourceList.add(resource);
    }

    int createGroups(JsCssMinConfig config) {
        int size = 0;
        for (Resource resource : resourceList) {
            ResourceType type = resource.getType();

            ResourceGroup checkResource = typesMap.get(type);
            if (checkResource == null)
                typesMap.put(type, ResourceUtils.createResourceGroup(type));

            ResourceGroup group = typesMap.get(type);
            if (config.isSkipRepetition() && resource.equals(group.getLastResource()))
                continue;
            group.addResource(resource);
            size++;
        }
        return size;
    }


    public String getBlockContent(String version) {
        StringBuilder results = new StringBuilder();
        for (ResourceType resourceType : typesMap.keySet()) {
            ResourceGroup resourceGroup = typesMap.get(resourceType);

            results.append(resourceGroup.getPackageLine(version)).append("\n");
        }
        return results.toString();
    }


    public void processBlock(JsCssProcessor processor) {
//        processResources(processor);
        createGroups(processor.getConfig());
        for (ResourceType resourceType : typesMap.keySet()) {
            ResourceGroup resourceGroup = typesMap.get(resourceType);
            resourceGroup.processFiles(processor);
        }
    }

    public Map<ResourceType, ResourceGroup> getTypesMap() {
        return typesMap;
    }


    public int getResourceCount() {
        return resourceList.size();
    }

    /**
     * Test method
     *
     * @return
     */
    List<Resource> getResourceList() {
        return resourceList;
    }
}
